package tk.mybatis.springboot.api.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;
import tk.mybatis.springboot.constants.RequestLimit;
import tk.mybatis.springboot.model.Accurate;
import tk.mybatis.springboot.model.CarInfo;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.model.Reinsurance;
import tk.mybatis.springboot.model.TWeixinInsurance;
import tk.mybatis.springboot.model.TWeixinOwner;
import tk.mybatis.springboot.model.TWeixinTruckInfo;
import tk.mybatis.springboot.model.TWeixinUser;
import tk.mybatis.springboot.service.WexinInsuranceService;
import tk.mybatis.springboot.wexin.bean.Userinfo;

/**
 * @author liujh
 */
@Controller
public class WexinInsuranceController extends BaseController{
	@Autowired
	private WexinInsuranceService wexinInsuranceService;
	//获取微信用户历史车牌号
    @RequestMapping(value = "weixin/insurance/getWeixinUser")
    @ResponseBody
    public Message getPersonInfo(HttpServletRequest request, HttpServletResponse response)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	List<TWeixinUser> weixinUsers = Lists.newArrayList();
    	if(userinfo != null){
    		weixinUsers = wexinInsuranceService.getWeixinUsers(userinfo.getOpenid());
    	}
    	return success("成功",weixinUsers);
    }
    
    //保存用户车牌号
    @RequestMapping(value = "quote/simulate/licenseno")
    @ResponseBody
    public Message saveTruckCard(HttpServletRequest request, HttpServletResponse response, String licenseNo, String finCityId, String provinceID)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(userinfo != null){
    		return wexinInsuranceService.saveTruckCard(request,userinfo.getOpenid(), licenseNo, finCityId, provinceID);
    	}
    	return error_msg("网络异常");
    }
    
    //保存用户车牌号
    @RequestMapping(value = "/insurance/insurancetypes")
    @ResponseBody
    public Message getInsurancetypes(HttpServletRequest request, HttpServletResponse response)throws Exception {  
    		return wexinInsuranceService.getInsurancetypes();
    }
    
    //查询省
    @RequestMapping(value = "/province")
    @ResponseBody
    public Message getProvince(HttpServletRequest request, HttpServletResponse response)throws Exception {  
    		return wexinInsuranceService.getProvince();
    }
    
    //根据省查询市
    @RequestMapping(value = "/city")
    @ResponseBody
    public Message getCity(HttpServletRequest request, HttpServletResponse response, String provinceId)throws Exception {  
    		return wexinInsuranceService.getCity(provinceId);
    }
    
    //模糊查询车辆信息
    @RequestMapping(value = "/car/query")
    @ResponseBody
    public Message carQuery(HttpServletRequest request, HttpServletResponse response, String brandName, String page)throws Exception {  
    		return wexinInsuranceService.carQuery(brandName, page);
    }
    
    // 更改车辆信息查询
    @RequestMapping(value = "/quote/simulate/vehicle")
    @ResponseBody
    public Message vehicle(HttpServletRequest request, HttpServletResponse response, CarInfo carInfo)throws Exception {  
    		return wexinInsuranceService.vehicle(request, carInfo);
    }
    
    // 更改保险信息
    @RequestMapping(value = "/quote/simulate/update")
    @ResponseBody
    public Message update(HttpServletRequest request, HttpServletResponse response, @RequestBody  JSONObject obj)throws Exception {  
    		return wexinInsuranceService.update(request, obj);
    }
    
    // 短信验证码
    @RequestMapping(value = "verify_code/send")
    @ResponseBody
    public Message send(HttpServletRequest request, HttpServletResponse response, String mobile)throws Exception {  
    		return wexinInsuranceService.send(mobile);
    }
    
    // 精准报价
    @RequestMapping(value = "/quote/accurate")
    @ResponseBody
    public Message accurate(HttpServletRequest request, HttpServletResponse response, Accurate accurate)throws Exception {  
    		return wexinInsuranceService.accurate(request, accurate);
    }
    
    // 地址选择
    @RequestMapping(value = "/dict/list")
    @ResponseBody
    public Message dictList(HttpServletRequest request, HttpServletResponse response)throws Exception {  
    		return wexinInsuranceService.dictList();
    }
    
    // 核保
    @RequestMapping(value = "/insurance/reinsurance")
    @ResponseBody
    @RequestLimit(limit=30,time=3600000)
    public Message reinsurance(HttpServletRequest request, HttpServletResponse response, Reinsurance reinsurance)throws Exception {  
    		return wexinInsuranceService.reinsurance(request, reinsurance);
    }
    
    //保存用户车辆信息
    @RequestMapping(value = "saveTruckInfo")
    @ResponseBody
    public Message saveTruckInfo(HttpServletRequest request, HttpServletResponse response, String userId, 
    		String vin, String engine, Date creationdate, Integer istransfer, Date istransferdate)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TWeixinTruckInfo weixinTruckInfo = new TWeixinTruckInfo();
    	if(userinfo != null){
    		weixinTruckInfo = wexinInsuranceService.saveTruckInfo(userId, vin, engine, creationdate, istransfer, istransferdate);
    	}
    	return success("成功",weixinTruckInfo);
    }
    
    //获取用户车辆信息
    @RequestMapping(value = "getTruckInfo")
    @ResponseBody
    public Message getTruckInfo(HttpServletRequest request, HttpServletResponse response, String userId)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TWeixinTruckInfo weixinTruckInfo = new TWeixinTruckInfo();
    	if(userinfo != null){
    		weixinTruckInfo = wexinInsuranceService.getTruckInfo(userId);
    	} 
    	return success("成功",weixinTruckInfo);
    }
    
    //获取车主信息
    @RequestMapping(value = "getOwner")
    @ResponseBody
    public Message getOwner(HttpServletRequest request, HttpServletResponse response, String userId)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TWeixinOwner weixinOwner = new TWeixinOwner();
    	if(userinfo != null){
    		weixinOwner = wexinInsuranceService.getOwner(userId);
    	}
    	return success("成功",weixinOwner);
    }
    
    //保存车主信息 
    @RequestMapping(value = "saveOwner")
    @ResponseBody
    public Message saveOwner(HttpServletRequest request, HttpServletResponse response, String userId, String ownerName, String idCard, String phone)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TWeixinOwner weixinOwner = new TWeixinOwner();
    	if(userinfo != null){
    		weixinOwner = wexinInsuranceService.saveOwner(userId, ownerName, idCard, phone);
    	}
    	return success("成功",weixinOwner);
    }
    
    //获取车险
    @RequestMapping(value = "getInsurance")
    @ResponseBody
    public Message getInsurance(HttpServletRequest request, HttpServletResponse response, String userId)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TWeixinInsurance weixinInsurance = new TWeixinInsurance();
    	if(userinfo != null){
    		weixinInsurance = wexinInsuranceService.getInsurance(userId);
    	}
    	return success("成功",weixinInsurance);
    }
    
    //保存车险
    @RequestMapping(value = "saveInsurance")
    @ResponseBody
    public Message saveInsurance(HttpServletRequest request, HttpServletResponse response, TWeixinInsurance info)throws Exception {  	
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TWeixinInsurance weixinInsurance = new TWeixinInsurance();
    	if(userinfo != null){
    		weixinInsurance = wexinInsuranceService.saveInsurance(info);
    	}
    	return success("成功",weixinInsurance);
    }
}
